package com.goal98.team.controllers

import com.goal98.team.domain.Authority
import com.goal98.team.domain.Person
import com.goal98.team.Constants

class PlayerController {

    def myteamService
    def authenticateService
    def personService
    def accountService
    def playerService

    static Map allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

    def index = { redirect action: list, params: params }

    def list = {
        [personList: myteamService.playersInMyTeam().sort{n1, n2->
            n1.id <=> n2.id    
        }]
    }

    def show = {
        def person = Person.get(params.id)
        if (!person) {
            flash.message = "Person not found with id $params.id"
            redirect action: list
            return
        }
        checkPlayerInMyTeam(params.id)

        List roleNames = []
        for (role in person.authorities) {
            roleNames << role.description
        }
        roleNames.sort {n1, n2 ->
            n1 <=> n2
        }

        [person: person, roleNames: roleNames]
    }

    def edit = {

        def person = Person.get(params.id)
        if (!person) {
            flash.message = "Person not found with id $params.id"
            redirect action: list
            return
        }

        checkPlayerInMyTeam(params.id)

        [person: person]
    }

    def checkPlayerInMyTeam = {id ->
        if (ifNotGranted(role: Constants.ROLE_ADMIN) && !myteamService.inMyTeam(id)) {
            flash.message = "Player with id $params.id is not in your team."
            redirect action: list
            return
        }

    }

    def create = {
        [person: new Person(params)]
    }

    def save = {

        def person = new Person()
        person.properties = params
        person.team = myteamService.getMyteam()
//		person.passwd = authenticateService.encodePassword(params.passwd)
        if (person.save()) {
            addRoles(person)
            redirect action: show, id: person.id
        }
        else {
            render view: 'create', model: [person: person]
        }
    }

    private void addRoles(person) {
        Authority.findByAuthority(Constants.ROLE_BASIC_MEMBER).addToPeople(person)
    }

    def update = {

        def person = Person.get(params.id)
        if (!person) {
            flash.message = "Person not found with id $params.id"
            redirect action: edit, id: params.id
            return
        }

        checkPlayerInMyTeam(params.id)

        long version = params.version.toLong()
        if (person.version > version) {
            person.errors.rejectValue 'version', "person.optimistic.locking.failure",
                    "Another user has updated this Person while you were editing."
            render view: 'edit', model: buildPersonModel(person)
            return
        }

        person.properties = params

        if (person.save()) {
            Authority.findAll().each { it.removeFromPeople(person) }
            addRoles(person)
            redirect action: show, id: person.id
        }
        else {
            render view: 'edit', model: [person: person]
        }
    }

    def account = {
        if(!params.id)
            params.id = authenticateService.userDomain()?.id
        def person = Person.get(params.id)
        if(person.account == null){
            personService.openAccount(person)    
        }
        def view = session.mobile?'m-account':'account'
        render(view:view, model:[person:person])
    }

    def credit = {
        def amount = params.amount as BigDecimal
        def description = params.description
        def person = Person.get(params.'person.id')
        playerService.credit(person, amount, description)
//        def view = session.mobile?'m-account':'account'
        redirect(action:'account', id:person.id)
    }
}
